//Drawing functions

#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <list>
#include <gl\glu.h>			// Header File For The GLu32 Library
#include "draw.h"			// Header File For The Draw Functions

const float LINE_WIDTH = 1.0f;
const float PI = 3.1415f;


int DrawPyramid()
{
	glBegin(GL_TRIANGLES);								// Start Drawing A Triangle
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Front)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Left Of Triangle (Front)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Right Of Triangle (Front)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Right)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Left Of Triangle (Right)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f( 1.0f,-1.0f, -1.0f);					// Right Of Triangle (Right)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Back)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f( 1.0f,-1.0f, -1.0f);					// Left Of Triangle (Back)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f(-1.0f,-1.0f, -1.0f);					// Right Of Triangle (Back)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Left)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Left Of Triangle (Left)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Right Of Triangle (Left)
	glEnd();											// Done Drawing The Pyramid
	
	return 0;

}

int DrawCube(float x, float y, float scale,bool hit)
{
	glLoadIdentity();									// Reset The Current Modelview Matrix
	glTranslatef(x, y ,-7.0f);                          // Move Right 1.5 Units And Into The Screen 7.0
	glScalef(scale,scale,scale);
	glLineWidth(LINE_WIDTH);  //Set the Line Width
	if (!hit)
		glColor3f( 1.0f , 1.0f , 1.0f );	//Set the Color
	else
		glColor3f( 1.0f, 0.0f, 0.0f);

	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Top Right Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Top Left Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Bottom Left Of The Quad (Top)
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Bottom Right Of The Quad (Top)
	glEnd();
	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Top Right Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Top Left Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Bottom Left Of The Quad (Bottom)
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Bottom Right Of The Quad (Bottom)
	glEnd();
	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Top Right Of The Quad (Front)
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Top Left Of The Quad (Front)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Bottom Left Of The Quad (Front)
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Bottom Right Of The Quad (Front)
		glVertex3f( 1.0f, 1.0f, 1.0f);
	glEnd();
	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Top Right Of The Quad (Back)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Top Left Of The Quad (Back)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Bottom Left Of The Quad (Back)
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Bottom Right Of The Quad (Back)
		glVertex3f( 1.0f,-1.0f,-1.0f);
	glEnd();
	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Top Right Of The Quad (Left)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Top Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Bottom Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Bottom Right Of The Quad (Left)
	glEnd();
	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Top Right Of The Quad (Right)
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Top Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Bottom Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Bottom Right Of The Quad (Right)
	glEnd();

	return 0;
}

float transformX(float x)
{
    return ((x*2) - 1) * 2.5f;
}

float transformY(float y)
{
	return -((y*2) - 1) * 2.5f;
}

int DrawMeters(float player1val, float player2val)
{
	//0 = player 1, 1 = player 2
	float r = 1.0f;
	float g = 1.0f; 
	float b = 1.0f;
	
	glLoadIdentity();									// Reset The Current Modelview Matrix
	glLineWidth(LINE_WIDTH);  //Set the Line Width

	glColor3f( r , g , b );	//Set the Color

	glTranslatef(0, 0, -7.0f); //Set it back a bit

	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f(-3.05f, 2.0f, 0.0f);					// Top Right Of The Quad (Top)
		glVertex3f(-3.05f,-2.0f, 0.0f);					// Top Left Of The Quad (Top)
		glVertex3f(-2.95f,-2.0f, 0.0f);					// Bottom Left Of The Quad (Top)
		glVertex3f(-2.95f, 2.0f, 0.0f);					// Bottom Right Of The Quad (Top)
		glVertex3f(-3.05f, 2.0f, 0.0f);					// Top Right Of The Quad (Top)
	glEnd();

	glBegin(GL_LINE_STRIP);									// Draw A Quad
		glVertex3f( 3.05f, 2.0f, 0.0f);					// Top Right Of The Quad (Top)
		glVertex3f( 3.05f,-2.0f, 0.0f);					// Top Left Of The Quad (Top)
		glVertex3f( 2.95f,-2.0f, 0.0f);					// Bottom Left Of The Quad (Top)
		glVertex3f( 2.95f, 2.0f, 0.0f);					// Bottom Right Of The Quad (Top)
		glVertex3f( 3.05f, 2.0f, 0.0f);					// Top Right Of The Quad (Top)
	glEnd();

	//Draw Game Bounds - Possibly Misplaced
	glBegin(GL_LINE_STRIP);
	    glVertex3f( -2.5f, 2.5f, 0.0f);					// Top Right Of The Quad (Top)
		glVertex3f( 2.5f, 2.5f, 0.0f);					// Top Left Of The Quad (Top)
		glVertex3f( 2.5f, -2.5f, 0.0f);					// Bottom Left Of The Quad (Top)
		glVertex3f( -2.5f, -2.5f, 0.0f);					// Bottom Right Of The Quad (Top)
		glVertex3f( -2.5f, 2.5f, 0.0f);					// Top Right Of The Quad (Top)
	glEnd();

	glLineWidth(4.0f);

	b = 0.0f;

	glColor3f( r , g , b );	//Set the Color

	glBegin(GL_LINES);
		glVertex3f(-3.05f, (-2 + (4 * player1val)), 0.0f);
		glVertex3f(-2.95f, (-2 + (4 * player1val)), 0.0f);
	glEnd();

	r = 0.0f;
	b = 1.0f;

	glColor3f( r , g , b );	//Set the Color

	glBegin(GL_LINES);
		glVertex3f( 3.05f, (-2 + (4 * player2val)), 0.0f);
		glVertex3f( 2.95f, (-2 + (4 * player2val)), 0.0f);
	glEnd();

	r = 1.0f;

	glColor3f( r , g , b );	//Set the Color

	glBegin(GL_LINES);
		glVertex3f( -2.5*16/20, 2.5, 0.0f);
		glVertex3f( -2.5*16/20, -2.5, 0.0f);
	glEnd();

	glBegin(GL_LINES);
		glVertex3f( 2.5*16/20, 2.5, 0.0f);
		glVertex3f( 2.5*16/20, -2.5, 0.0f);
	glEnd();

	return 0;
}
int DrawSphere(float x, float y, float rad, GLUquadric &quadratic, int type)
{
	float r, g, b;
	switch (type)
	{
	case 0: //Gravity
		r = 0.0f;
		g = 0.0f; 
		b = 1.0f;
		break;
	case 1: //Damage
		r = 1.0f;
		g = 0.0f; 
		b = 0.0f;
		break;
	case 2: //Power Up
		r = 0.0f;
		g = 1.0f; 
		b = 0.0f;
		break;
	case 3: //Player1
		r = 1.0f;
		g = 1.0f; 
		b = 0.0f;
		break;
	case 4: //Player2
		r = 0.0f;
		g = 1.0f; 
		b = 1.0f;
		break;
	case 5: //Ball
		r = 0.5f;
		g = 0.5f;
		b = 0.5f;
		break;
	default: //woo!
		r = 1.0f;
		g = 1.0f; 
		b = 1.0f;
		break;
	}

	//Transform the x and y coords

	//float xcoord = transformX(x);
	//float ycoord = transformY(y);

	glLoadIdentity();									// Reset The Current Modelview Matrix
	glTranslatef(x, y, -7.0f);                          // Move Right 1.5 Units And Into The Screen 7.0

	glLineWidth(LINE_WIDTH);  //Set the Line Width

	glColor3f( r , g , b );	//Set the Color

	//DRAW SPHERE
	gluSphere(&quadratic,rad * 5 ,8,8);

	return 0;
}
int DrawTorus(float x, float y, GLUquadric &quadratic, float inRad, float outRad, float scale, bool hit)
{
	glLoadIdentity();
	glTranslatef(x,y,-7.0f);
	glRotatef(275,0.0f,1.0f,0.0f);
	glScalef(scale,scale,scale);
	glEnable(GL_POLYGON_SMOOTH);
	glLineWidth(LINE_WIDTH+2);
	if (!hit)
		glColor3f(1.0f,1.0f,1.0f);
	else
		glColor3f(0.0f,1.0f,0.0f);
	gluDisk(&quadratic,inRad*scale,outRad*scale,10,2);
	return 0;
}
int DrawTerrain(std::list<float> &heights)
{
	//Draw Terrain Here
	float r = 1.0f;
	float g = 1.0f; 
	float b = 1.0f;
	
	glLoadIdentity();									// Reset The Current Modelview Matrix
	glLineWidth(LINE_WIDTH);  //Set the Line Width

	glColor3f( r , g , b );	//Set the Color

	glTranslatef(0, 0, -7.0f); //Set it back a bit

	glBegin(GL_LINE_STRIP);	// Draw the Terrain Strip
		int i = 0;
		for(std::list<float>::iterator itr = heights.begin();
			itr != heights.end(); itr++)
		{
			glVertex3f( 3.0f - ( 6.0f / heights.size() ) * i, ((*itr/60.0f)-1.0f)*2,0.0f);
			i++;
		}
	glEnd();
	
	return 0;
}

int DrawString(std::string text, float posx, float posy, GLuint base)
{
	glLoadIdentity();									// Reset The Current Modelview Matrix
	glTranslatef(0.0f,0.0f,-1.0f);						// Move One Unit Into The Screen
	// Pulsing Colors Based On Text Position
	glColor3f(1.0f, 1.0f, 1.0f);
	// Position The Text On The Screen
	glRasterPos2f(posx, posy);
 	glPrint(base, text.c_str());	// Print GL Text To The Screen
	return 0;
}

GLvoid glPrint(GLuint base, const char *fmt, ...)					// Custom GL "Print" Routine
{
	char		text[256];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

	if (fmt == NULL)									// If There's No Text
		return;											// Do Nothing

	va_start(ap, fmt);									// Parses The String For Variables
	    vsprintf_s(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(base - 32);								// Sets The Base Character to 32
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}
int DrawProgress(float lBound, float uBound, float target, float uLX, float uLY, float dRX, float dRY)
{
	glLoadIdentity();									// Reset The Current Modelview Matrix
	float percentage = target/(uBound-lBound);
	if (percentage < .25)
		glColor3f(1.0f,0.0f,0.0f);
	if (percentage < .75 && percentage >= .25)
		glColor3f(0.97f,0.49f,.06f); // orangey color
	if (percentage <= 1 && percentage >= .75)
		glColor3f(0.0f,1.0f,0.0f);
	glTranslatef(uLX, uLY ,-7.0f);                          // Move Right 1.5 Units And Into The Screen 7.0
	glBegin(GL_QUADS);
	glVertex2f(uLX+(8*percentage), uLY);
	glVertex2f(dRX, uLY);
	glVertex2f(dRX, dRY);
	glVertex2f(uLX+(8*percentage), dRY);
	glEnd();
	return 0;
}
int DrawButton(bool pressed, float x1, float y1, float x2, float y2)
{
	glLoadIdentity();
	glTranslatef(x1,y1,-0.7f);
	glTranslatef(x2,y2,-0.7f);
	if (pressed)
		glColor3f(0.87f,0.04f,0.72f);
	else
		glColor3f(0.09f,0.17f,0.83f);
	glBegin(GL_QUADS);
	glVertex2f(x1,y1);
	glVertex2f(x2,y1);
	glVertex2f(x2,y2);
	glVertex2f(x1,y2);
	glEnd();
	return 0;
}
int DrawPillBoxes(int numPills, float x, float y)
{
	glLoadIdentity();
	glTranslatef(x,y,-0.7f);
	glScalef(0.1f,0.1f,0.1f);
	glColor3f(0.14f,0.937f,1.0f);
	float mod = 0.1f;
	for (int i = 0; i < numPills; i++)
	{
		glBegin(GL_QUADS);
		glVertex2f(x+(mod*i),y);
		glVertex2f(x+(mod*i)+.1f,y+.1f);
		glVertex2f(x+(mod*i)+.1f,y);
		glVertex2f(x+(mod*i),y+.1f);
		glEnd();
	}
	return 0;
}